<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
    <title>Coverage Report :: SpriteStore</title>
    <style type="text/css">
        @import "../../.css/coverage.css";
    </style>
</head>

<body>
<div class="header"></div>

<div class="content">
    <div class="breadCrumbs">
        [ <a href="../../index.html">all classes</a> ]
        [ <a href="../index.html">nl.tudelft.jpacman.sprite</a> ]
    </div>

    <h1>Coverage Summary for Class: SpriteStore (nl.tudelft.jpacman.sprite)</h1>

    <table class="coverageStats">
        <tr>
            <th class="name">Class</th>
            <th class="coverageStat
">
                Class, %
            </th>
            <th class="coverageStat
">
                Method, %
            </th>
            <th class="coverageStat
">
                Line, %
            </th>
        </tr>
        <tr>
            <td class="name">SpriteStore</td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 1)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 5)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 23)
  </span>
            </td>
        </tr>

    </table>

    <br/>
    <br/>


    <div class="sourceCode"><i>1</i>&nbsp;package nl.tudelft.jpacman.sprite;
        <i>2</i>&nbsp;
        <i>3</i>&nbsp;import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
        <i>4</i>&nbsp;
        <i>5</i>&nbsp;import java.awt.image.BufferedImage;
        <i>6</i>&nbsp;import java.io.IOException;
        <i>7</i>&nbsp;import java.io.InputStream;
        <i>8</i>&nbsp;import java.util.HashMap;
        <i>9</i>&nbsp;import java.util.Map;
        <i>10</i>&nbsp;
        <i>11</i>&nbsp;import javax.imageio.ImageIO;
        <i>12</i>&nbsp;
        <i>13</i>&nbsp;/**
        <i>14</i>&nbsp; * Utility to load {@link Sprite}s.
        <i>15</i>&nbsp; *
        <i>16</i>&nbsp; * @author Jeroen Roosen
        <i>17</i>&nbsp; */
        <b class="nc"><i>18</i>&nbsp;public class SpriteStore {</b>
        <i>19</i>&nbsp;
        <i>20</i>&nbsp; /**
        <i>21</i>&nbsp; * We only need to load images once, so we keep track
        <i>22</i>&nbsp; * of them in a hash map.
        <i>23</i>&nbsp; */
        <i>24</i>&nbsp; private final Map&lt;String, Sprite&gt; spriteMap;
        <i>25</i>&nbsp;
        <i>26</i>&nbsp; /**
        <i>27</i>&nbsp; * Create a new sprite store.
        <i>28</i>&nbsp; */
        <b class="nc"><i>29</i>&nbsp; public SpriteStore() {</b>
        <b class="nc"><i>30</i>&nbsp; spriteMap = new HashMap&lt;&gt;();</b>
        <b class="nc"><i>31</i>&nbsp; }</b>
        <i>32</i>&nbsp;
        <i>33</i>&nbsp; /**
        <i>34</i>&nbsp; * Loads a sprite from a resource on the class path.
        <i>35</i>&nbsp; * Sprites are loaded once, and then stored in the store
        <i>36</i>&nbsp; * so that they can be efficiently retrieved.
        <i>37</i>&nbsp; *
        <i>38</i>&nbsp; * @param resource
        <i>39</i>&nbsp; * The resource path.
        <i>40</i>&nbsp; * @return The sprite for the resource.
        <i>41</i>&nbsp; * @throws IOException
        <i>42</i>&nbsp; * When the resource could not be loaded.
        <i>43</i>&nbsp; */
        <i>44</i>&nbsp; public Sprite loadSprite(String resource) throws IOException {
        <b class="nc"><i>45</i>&nbsp; Sprite result = spriteMap.get(resource);</b>
        <b class="nc"><i>46</i>&nbsp; if (result == null) {</b>
        <b class="nc"><i>47</i>&nbsp; result = loadSpriteFromResource(resource);</b>
        <b class="nc"><i>48</i>&nbsp; spriteMap.put(resource, result);</b>
        <i>49</i>&nbsp; }
        <b class="nc"><i>50</i>&nbsp; return result;</b>
        <i>51</i>&nbsp; }
        <i>52</i>&nbsp;
        <i>53</i>&nbsp; /**
        <i>54</i>&nbsp; * Loads a sprite from a resource on the class path.
        <i>55</i>&nbsp; *
        <i>56</i>&nbsp; * @param resource
        <i>57</i>&nbsp; * The resource path.
        <i>58</i>&nbsp; * @return A new sprite for the resource.
        <i>59</i>&nbsp; * @throws IOException
        <i>60</i>&nbsp; * When the resource could not be loaded.
        <i>61</i>&nbsp; */
        <i>62</i>&nbsp; @SuppressFBWarnings(
        <i>63</i>&nbsp; value = &quot;RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE&quot;,
        <i>64</i>&nbsp; justification = &quot;false positive in java 11&quot;
        <i>65</i>&nbsp; )
        <i>66</i>&nbsp; private Sprite loadSpriteFromResource(String resource) throws IOException {
        <b class="nc"><i>67</i>&nbsp; try (InputStream input = SpriteStore.class.getResourceAsStream(resource)) {</b>
        <b class="nc"><i>68</i>&nbsp; if (input == null) {</b>
        <b class="nc"><i>69</i>&nbsp; throw new IOException(&quot;Unable to load &quot; + resource + &quot;, resource
            does not exist.&quot;);</b>
        <i>70</i>&nbsp; }
        <b class="nc"><i>71</i>&nbsp; BufferedImage image = ImageIO.read(input);</b>
        <b class="nc"><i>72</i>&nbsp; return new ImageSprite(image);</b>
        <b class="nc"><i>73</i>&nbsp; }</b>
        <i>74</i>&nbsp; }
        <i>75</i>&nbsp;
        <i>76</i>&nbsp; /**
        <i>77</i>&nbsp; * Creates a new {@link AnimatedSprite} from a base image.
        <i>78</i>&nbsp; *
        <i>79</i>&nbsp; * @param baseImage
        <i>80</i>&nbsp; * The base image to convert into an animation.
        <i>81</i>&nbsp; * @param frames
        <i>82</i>&nbsp; * The amount of frames of the animation.
        <i>83</i>&nbsp; * @param delay
        <i>84</i>&nbsp; * The delay between frames.
        <i>85</i>&nbsp; * @param loop
        <i>86</i>&nbsp; * Whether this sprite is a looping animation or not.
        <i>87</i>&nbsp; * @return The animated sprite.
        <i>88</i>&nbsp; */
        <i>89</i>&nbsp; public AnimatedSprite createAnimatedSprite(Sprite baseImage, int frames,
        <i>90</i>&nbsp; int delay, boolean loop) {
        <b class="nc"><i>91</i>&nbsp; assert baseImage != null;</b>
        <b class="nc"><i>92</i>&nbsp; assert frames &gt; 0;</b>
        <i>93</i>&nbsp;
        <b class="nc"><i>94</i>&nbsp; int frameWidth = baseImage.getWidth() / frames;</b>
        <i>95</i>&nbsp;
        <b class="nc"><i>96</i>&nbsp; Sprite[] animation = new Sprite[frames];</b>
        <b class="nc"><i>97</i>&nbsp; for (int i = 0; i &lt; frames; i++) {</b>
        <b class="nc"><i>98</i>&nbsp; animation[i] = baseImage.split(i * frameWidth, 0, frameWidth,</b>
        <b class="nc"><i>99</i>&nbsp; baseImage.getHeight());</b>
        <i>100</i>&nbsp; }
        <i>101</i>&nbsp;
        <b class="nc"><i>102</i>&nbsp; return new AnimatedSprite(animation, delay, loop);</b>
        <i>103</i>&nbsp; }
        <i>104</i>&nbsp;
        <i>105</i>&nbsp;}
    </div>
</div>

<div class="footer">

    <div style="float:right;">generated on 2023-04-25 16:45</div>
</div>
</body>
</html>
